/* eslint-disable no-param-reassign */
import Vue from 'vue'
const initDrawerConfig = {
    // 右侧抽屉框要呈现的组件
    compName: null,
    title: '主标题',
    // 右侧抽屉框头部区域右边内容
    subTitle: '',
    // 右侧抽屉框底部区域的操作按钮配置
    footerButtons: [],
    width: 'medium',
    // 动态组件需要绑定的参数
    vars: null,
    // 调用右侧抽屉框的组件实例
    vueInstance: null,
    // 是否需要取消按钮
    hasCancelBtn: true,
    // 右侧抽屉框头部区域的操作按钮配置
    headerButtons: [],
    showFooter: true,
    createdTime: 8888,
}
const app = {
    state: {
        innerLoginVisible: false,
        ssoLoginVisible: false,
        rightDrawerOPen: false, // 右侧弹框是否打开
        dictTypeList: {}, // 字典列表
        locale: '', // 中英文
        newDataCache: {}, // 新建数据的缓存
        rightDrawerLoading: false, // 右侧弹框确定按钮的loading状态
        rightSubmitDisabled: true, // 右侧弹框确定按钮的禁用状态
        drawerConfig: {...initDrawerConfig },
        drawerMainOpened: false,
        instantUpdateTime: Date.now(), // 即时更新时间
    },
    mutations: {
        // 重置状态
        RESET_APP_STATE: (state) => {
            state.drawerMainOpened = false
            state.dictTypeList = {} // 字典列表
            state.locale = ''
            state.newDataCache = {} // 新建数据的缓存
        },
        SET_rightDrawerOPen: (state, rightDrawerOPen) => {
            state.rightDrawerOPen = rightDrawerOPen
        },
        SET_DICT_TYPE: (state, dictTypeList) => {
            state.dictTypeList = dictTypeList
        },
        SET_LANG: (state, locale) => {
            state.locale = locale
            Vue.ls.set('locale', locale)
        },
        SET_newDataCache: (state, initialValue) => {
            if (initialValue.key) {
                Vue.set(state.newDataCache, `${initialValue.key}`, initialValue.value)
            } else {
                state.newDataCache = {}
                state.drawerMainOpened = false
            }
        },
        SET_rightDrawerLoading(state, val) {
            state.rightDrawerLoading = val
        },
        SET_rightSubmitDisabled(state, val) {
            state.rightSubmitDisabled = val
        },
        // 折叠右侧抽屉弹框
        toggleDrawerMain(state, val) {
            state.drawerMainOpened = val
        },
        // 修改右侧抽屉弹框配置
        setDrawerConfig(state, config) {
            state.drawerConfig = {...initDrawerConfig, ...config, createdTime: Date.now() }
        },
        // 重置右侧抽屉弹框配置
        resetDrawerConfig(state, loadCom) {
            const data = {...initDrawerConfig }
            if (loadCom) {
                data.width = state.drawerConfig.width
                data.vars = { loading: true }
                data.compName = loadCom
            }
            state.drawerConfig = {...data }
        },
        // 设置即时更新时间
        setInstantUpdateTime(state) {
            state.instantUpdateTime = Date.now()
        },
        toggleInnerLogin(state, val) {
          state.innerLoginVisible = val
        },
        toggleSsoLogin(state, val) {
          state.ssoLoginVisible = val
        },
    },
    actions: {
        // 获取字典列表
        initDictType({ commit }) {
            Vue.prototype.$api.getDictType().then((response) => {
                if (!response) {
                    // 由于mockjs 不支持自定义状态码只能这样hack
                } else {
                    const data = response
                    commit('SET_DICT_TYPE', data)
                }
            })
        },
    },
}

export default app